<html>
<head>
    <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>CentOS 7定时执行Java程序 | |ω･`)暗中观察</title>
<meta name="description" content="大爷不进来看看吗？这里有好康的哦~~~" />
<link rel="shortcut icon" href="https://Yanoona.github.io/favicon.ico">
<link rel="stylesheet" href="https://Yanoona.github.io/styles/main.css">

<script src="https://Yanoona.github.io/media/js/jquery.min.js"></script>
<script src="https://Yanoona.github.io/media/js/masonry.pkgd.min.js"></script>
<script src="https://Yanoona.github.io/media/js/aos.js"></script>
<script src="https://Yanoona.github.io/media/js/pace.min.js"></script>
<script src="https://Yanoona.github.io/media/js/view-image.min.js"></script>
<script src="https://Yanoona.github.io/media/js/functions.js"></script>
    <script src="https://Yanoona.github.io/media/js/waterfall.min.js"></script>
    <script src="https://Yanoona.github.io/media/js/prism.min.js"></script>
</head>
<body>
<header id="header" class="grid-container">
    <!-- start: .menu-wrapper -->
    <div class="menu-mobile">
        <i class="fa fa-reorder"></i>
    </div>
    <div class="menu-wrapper">
        <div class="">
            <nav class="main-nav " >
                <ul id="menu-header" class="menu gradient-effect">
                    
                        <li class="">
                            <a href="https://Yanoona.github.io" class="menu">
                                首页
                            </a>
                        </li>
                    
                        <li class="">
                            <a href="https://Yanoona.github.io/archives" class="menu">
                                归档
                            </a>
                        </li>
                    
                        <li class="">
                            <a href="https://Yanoona.github.io/tags" class="menu">
                                标签
                            </a>
                        </li>
                    
                        <li class="">
                            <a href="https://github.com/Yanoona" class="menu">
                                关于
                            </a>
                        </li>
                    
                </ul>
            </nav>
            <!-- end: .main-nav -->
            <div class="clear"></div>
            <div class="border hide-on-tablet hide-on-mobile"></div>
        </div>
        <div class="clear"></div>
    </div>
    <!-- end: .menu-wrapper -->
    <div class="clear"></div>
</header>

<main id="single" class="main grid-container fullcover no-sidebar aos-init aos-animate" data-aos="fade">

    <div class="center content">
        <div class="featured-image cover"
             style="background-image: url('https://Yanoona.github.io/post-images/XvCd5eo8U.jpg');">
            <div class="meta top">
                <time class="meta-info" style="float:left;" datetime="2020-02-27"><i
                            class="fa fa-calendar"></i><span
                            class="lately">4 个月前</span></time>
            </div>
            <div class="info">
                <div class="tags ">
                    
                        <a href="https://Yanoona.github.io/wDtcCNomO/" class="ctag ctag-0 ctag-wDtcCNomO"
                           aria-label="">Crontab定时任务</a>
                    
                        <a href="https://Yanoona.github.io/java/" class="ctag ctag-1 ctag-java"
                           aria-label="">Java</a>
                    
                        <a href="https://Yanoona.github.io/j_dWapwnj/" class="ctag ctag-2 ctag-j_dWapwnj"
                           aria-label="">CentOS 7</a>
                    
                </div>
                <h1 class="title ularge white bold">CentOS 7定时执行Java程序</h1>
            </div>
        </div>
    </div>

    <div class="epcl-page-wrapper">
        <div class="left-content grid-70 np-mobile">
            <article class="main-article post">
                <section class="post-content">
                    <div class="text">
                        <h2 id="概述">😀概述</h2>
<p>记录一次CentOS7中使用Crontab定时任务执行Java程序的操作。</p>
<h2 id="准备">😃准备</h2>
<ul>
<li>JAVA程序(可运行的Jar包)</li>
<li>CentOS系统</li>
<li>Crontab定时任务服务</li>
</ul>
<h2 id="步骤">😄步骤</h2>
<h3 id="将jar包上传到centos系统上">将Jar包上传到CentOS系统上</h3>
<blockquote>
<p>此步省略详细操作，简述大概步骤🤐。</p>
</blockquote>
<h4 id="建立平台连接">建立平台连接</h4>
<p>为以后方便在Windows系统和CentOS系统间的文件传输操作，此步建议搭建👨‍🔧 <code>ftp</code>服务器。</p>
<h4 id="打包java程序">打包Java程序</h4>
<p>Maven方式：</p>
<p>在项目中的<strong>pom.xml</strong>文件中，加入以下👇打包标签，之后再使用打包命令<code>mvn clean package</code>或者使用IDEA里的Maven插件。</p>
<pre><code class="language-xml">&lt;build&gt;
        &lt;!--使用Maven编译可执行的jar --&gt;
        &lt;finalName&gt;update_ase_cms&lt;/finalName&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;appendAssemblyId&gt;false&lt;/appendAssemblyId&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;{在这里填入你的启动类}&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;make-assembly&lt;/id&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;assembly&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

</code></pre>
<blockquote>
<p>如果运行Jar包后出现<code>java.lang.ClassNotFoundException</code>或者<code>no main manifest attribute</code>异常☹️的话，就是此处没有配置好。</p>
</blockquote>
<h4 id="将jar包上传到centos中">将Jar包上传到CentOS中</h4>
<p>此步骤使用 ftp 服务器上传即可😤。</p>
<hr>
<h3 id="crontab配置和使用">Crontab配置和使用</h3>
<h4 id="查看是否安装crontab">查看是否安装Crontab</h4>
<pre><code>rpm -qa | grep cron
</code></pre>
<p>已安装的为下图😋所示（一般来说，系统自带Crontab😉）：</p>
<figure data-type="image" tabindex="1"><img src="https://Yanoona.github.io/post-images/1582792080190.png" alt="" loading="lazy"></figure>
<h4 id="查看crontab是否开机自启及相关命令">查看Crontab是否开机自启及相关命令</h4>
<pre><code>#查看状态
systemctl status crond
#开启crontab服务
systemctl start crond
#关闭crontab服务
systemctl stop crond
#重启crontab服务
systemctl restart crond
#开机启动
systemctl enable crond
#删除开机启动
systemctl disable crond
</code></pre>
<h4 id="编写shell脚本">编写shell脚本</h4>
<p>注意：目录必须为👏绝对路径👏。（执行前请注意自己是否配置了Java环境，并且配置成功了）</p>
<pre><code class="language-shell">#创建shell脚本文件
vim {你的脚本文件名}.sh

#在文件中输入以下命令

#!/bin/bash
#写入环境变量
export LANG=&quot;en_US.UTF-8&quot;
export JAVA_HOME={这里放你的Java地址}
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
java -jar {你上传的Jar包目录} &amp;&amp; echo &quot;开始执行项目！&quot;
echo &quot;【`date +%Y-%m-%d_%H:%M:%S`】完成！&quot;
</code></pre>
<h4 id="添加定时任务">添加定时任务</h4>
<p>打开定时任务编辑：</p>
<pre><code>crontab -e
</code></pre>
<p>在打开后的文本中编辑：</p>
<pre><code>*/1 * * * * {你的shell文件的绝对路径} &gt;&gt; {要将输出信息存储到日志文件的目录文件}
</code></pre>
<blockquote>
<ul>
<li>
<p>👩‍🏫👩‍🏫<code>{要将输出信息存储到日志文件的目录文件}</code>这里的意思就是<strong>每次执行时将shell脚本中的输出都追加到这个文件下</strong></p>
</li>
<li>
<p><code>*/1 * * * *</code>这里表示每隔1分钟就执行一次，具体用法如以下所示：</p>
</li>
</ul>
<p>⚡️<a href="https://www.e-learn.cn/content/qita/2644597">Crontab-易学教程</a> 或者 <a href="http://cron.qqe2.com/">Crontab表达式生成</a>⚡️</p>
</blockquote>
<h4 id="查看定时任务执行状态">查看定时任务执行状态</h4>
<h5 id="查看任务日志">查看任务日志：</h5>
<pre><code>tail -f /var/log/cron
</code></pre>
<figure data-type="image" tabindex="2"><img src="https://Yanoona.github.io/post-images/1582792071997.png" alt="" loading="lazy"></figure>
<p>其中如<code>Feb 27 15:57:25 hadoop104 crontab[21024]: (hadoop) REPLACE (hadoop)</code>等信息的作用为（借大佬😎说的）：</p>
<blockquote>
<p>Cron程序自己的日志，像是Cron daemon刚启动的时候打印的信息，类似于报告版本之类的，这里说此版本的crond支持inotify特性，也就是说，可以自动监测配置文件的改变（利用OS的inotify接口）而<code>Feb 27 16:00:01 hadoop104 CROND[21454]: (root) CMD (/usr/lib64/sa/sa1 1 1)</code>的作用为：</p>
<p>Cron在某个时间点运行了某个Crontab里CMD，这里的CMD名字叫做sa，是系统信息收集的后端，请参见sar/sa<code>Feb 27 16:06:01 hadoop104 CROND[21644]: (hadoop) CMD (/usr/java_program/crontab_java.sh &gt;&gt; /usr/java_program/file)</code>这一类信息表示：</p>
</blockquote>
<blockquote>
<p>在这个时间段执行的定时任务。</p>
</blockquote>
<h5 id="查看执行了的定时任务信息">查看执行了的定时任务信息</h5>
<p>因为之前在定时任务编辑时，我们写了✏️<code>{你的shell文件的绝对路径} &gt;&gt; {要将输出信息存储到日志文件的目录文件}</code>。本步骤就是查看👉<code>{要将输出信息存储到日志文件的目录文件}</code>这个文件里的信息。</p>
<p>进入到这个目录，如我这里的 /usr/java_program/file 文件。</p>
<p>打开如下图所示，表明任务执行成功，没有报错。🌈🌈</p>
<figure data-type="image" tabindex="3"><img src="https://Yanoona.github.io/post-images/1582792045981.png" alt="" loading="lazy"></figure>
<hr>
<h3 id="使用总结">使用总结</h3>
<ol>
<li>打包的时候，先在本地测试jar包能不能运行，会不会报异常，确认没有问题之后，方可上传。</li>
<li>如果在集群环境下设置定时任务，最好先确保每台机子的时区、时间是否一致。</li>
<li>路径使用绝对路径编写。</li>
</ol>
<p>就先记录这么多了，还没碰到的异常就等到以后使用的在记录，GoodBye！</p>

                    </div>
                    <div class="clear"></div>
                </section>
            </article>
            <div class="clear"></div>

            <section class="related section">
                
                    <article class="prev grid-50 tablet-grid-50 grid-parent">
                        <div class="thumb cover lazy loaded"
                             style="background-image: url('https://Yanoona.github.io/post-images/lIPSlIjZp.jpg');"></div>
                        <a href="https://Yanoona.github.io/lIPSlIjZp/" class="full-link"></a>
                        <div class="info">
                            <time datetime="2020-05-11">2020-05-11</time>
                            <h4 class="title white no-margin">Java8日期API使用记录</h4>
                        </div>
                        <span class="epcl-button red">
                  <img src="https://Yanoona.github.io/media/images/left-arrow.svg" width="15" alt="Left Arrow">
                </span>
                        <div class="overlay"></div>
                    </article>
                
                
                    <article class="next grid-50 tablet-grid-50 grid-parent">
                        <div class="thumb cover lazy loaded"
                             style="background-image: url('https://Yanoona.github.io/post-images/zxJ0ci4F1.jpg');"></div>
                        <a href="https://Yanoona.github.io/zxJ0ci4F1/" class="full-link"></a>
                        <div class="info">
                            <time datetime="2020-02-20">2020-02-20</time>
                            <h4 class="title white no-margin">使用axios出现的跨域问题</h4>
                        </div>
                        <span class="epcl-button red">
                  <img src="https://Yanoona.github.io/media/images/right-arrow.svg" width="15" alt="Left Arrow">
                </span>
                        <div class="overlay"></div>
                    </article>
                

                <div class="clear"></div>
            </section>
        </div>
    </div>
</main>

    <footer id="footer" class="grid-container">
        <div class="widgets row gradient-effect">
            <div class="default-sidebar border-effect">
              <div class="grid-33 tablet-grid-50 mobile-grid-100">
                <section id="tag_cloud-2" class="widget widget_epcl_posts_thumbs underline-effect">
                  <h4 class="widget-title title white bordered">最新文章</h4>
                  
                  
                  <article class="item post-0 post type-post status-publish format-standard has-post-thumbnail hentry">
                    <a href="https://Yanoona.github.io/2rO0MFRF0/" class="thumb hover-effect">
                      <span class="fullimage cover" style="display: block;background-image: url('https://Yanoona.github.io/post-images/2rO0MFRF0.jpg');"></span>
                    </a>
                    <div class="info gradient-effect">
                      <time datetime="2020-06-29">2020-06-29</time>
                      <h4 class="title usmall">
                        <a href="https://Yanoona.github.io/2rO0MFRF0/">Java8 Stream流学习笔记</a>
                      </h4>
                    </div>
                    <div class="clear"></div>
                  </article>
                  
                  
                  
                  <article class="item post-1 post type-post status-publish format-standard has-post-thumbnail hentry">
                    <a href="https://Yanoona.github.io/ta0wVkBzF/" class="thumb hover-effect">
                      <span class="fullimage cover" style="display: block;background-image: url('https://Yanoona.github.io/post-images/ta0wVkBzF.jpg');"></span>
                    </a>
                    <div class="info gradient-effect">
                      <time datetime="2020-05-13">2020-05-13</time>
                      <h4 class="title usmall">
                        <a href="https://Yanoona.github.io/ta0wVkBzF/">Spring 知识回顾之 体系结构</a>
                      </h4>
                    </div>
                    <div class="clear"></div>
                  </article>
                  
                  
                  
                  <article class="item post-2 post type-post status-publish format-standard has-post-thumbnail hentry">
                    <a href="https://Yanoona.github.io/CpjS5YWXn/" class="thumb hover-effect">
                      <span class="fullimage cover" style="display: block;background-image: url('https://Yanoona.github.io/post-images/CpjS5YWXn.jpg');"></span>
                    </a>
                    <div class="info gradient-effect">
                      <time datetime="2020-05-13">2020-05-13</time>
                      <h4 class="title usmall">
                        <a href="https://Yanoona.github.io/CpjS5YWXn/">Spring 知识回顾之 概念</a>
                      </h4>
                    </div>
                    <div class="clear"></div>
                  </article>
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  <div class="clear"></div>
                </section>
              </div>

              <div class="grid-33 tablet-grid-50 mobile-grid-100">
                <section id="tag_cloud-2" class="widget widget_tag_cloud underline-effect">
                  <h4 class="widget-title title white bordered">标签云</h4>
                  <div class="tagcloud">
                    
                      <a href="https://Yanoona.github.io/java/" class="ctag ctag-0 ctag-java" aria-label="">Java</a>
                    
                      <a href="https://Yanoona.github.io/7D-9V5P5T/" class="ctag ctag-1 ctag-7D-9V5P5T" aria-label="">Spring</a>
                    
                      <a href="https://Yanoona.github.io/wDtcCNomO/" class="ctag ctag-2 ctag-wDtcCNomO" aria-label="">Crontab定时任务</a>
                    
                      <a href="https://Yanoona.github.io/j_dWapwnj/" class="ctag ctag-3 ctag-j_dWapwnj" aria-label="">CentOS 7</a>
                    
                      <a href="https://Yanoona.github.io/dXBZlTYs0/" class="ctag ctag-4 ctag-dXBZlTYs0" aria-label="">Vue</a>
                    
                      <a href="https://Yanoona.github.io/bigData/" class="ctag ctag-5 ctag-bigData" aria-label="">大数据</a>
                    
                      <a href="https://Yanoona.github.io/Sqoop/" class="ctag ctag-6 ctag-Sqoop" aria-label="">Sqoop</a>
                    
                      <a href="https://Yanoona.github.io/Hive/" class="ctag ctag-7 ctag-Hive" aria-label="">Hive</a>
                    
                      <a href="https://Yanoona.github.io/mysql/" class="ctag ctag-8 ctag-mysql" aria-label="">mysql</a>
                    
                      <a href="https://Yanoona.github.io/Hadoop/" class="ctag ctag-9 ctag-Hadoop" aria-label="">Hadoop</a>
                    
                  </div>
                  <div class="clear"></div>
                </section>
              </div>

              <div class="grid-33 tablet-grid-50 mobile-grid-100">
                <section id="epcl_about-2" class="widget widget_epcl_about underline-effect">
                  <h4 class="widget-title title white bordered">关于我</h4>
                  <div class="avatar">
                    <a href="https://github.com/Yanoona" class="translate-effect thumb"><span class="fullimage cover" style="background-image: url(https://Yanoona.github.io/images/avatar.png);"></span></a>
                  </div>
                  <div class="info">
                    <h4 class="title small author-name gradient-effect no-margin"><a href="https://github.com/Yanoona">|ω･`)暗中观察</a></h4>
                    <p class="founder">大爷不进来看看吗？这里有好康的哦~~~</p>
                    <div class="social">
                      
                        
                            <a href="https://github.com/Yanoona" class="translate-effect" target="_blank"><i class="fa fa-github"></i></a>
                        
                      
                        
                      
                        
                            <a href="https://blog.csdn.net/qq_39299449" class="translate-effect" target="_blank"><i class="fa fa-csdn"></i></a>
                        
                      
                        
                            <a href="https://www.jianshu.com/u/6d8fc7359702" class="translate-effect" target="_blank"><i class="fa fa-jianshu"></i></a>
                        
                      
                    </div>
                  </div>
                  <div class="clear"></div>
                  </section>
              </div>

            </div>
            <div class="clear"></div>
        </div>

        <p class="published border-effect">
          ©2020 共 12 篇文章
        </p>
        
        <a href="javascript:void(0)" id="back-to-top" class="epcl-button dark" style="display:none">
          <i class="fa fa-arrow"></i>
        </a>
    </footer>
    <div class="clear"></div>

<script type="text/javascript">
    jQuery(document).ready(function ($) {
        $("photos img").each(function () {
            var _a = $("<a></a>").attr("href", this.src);
            $(this).wrap("<div class='photo'></div>").wrap(_a);
        })
        $("figure img").each(function () {
            var _b = $("<a></a>").attr("href", this.src);
            $(this).wrap(_b);
        })
        isImgLoad(function () {
            var photos = document.querySelector('photos');
            if (photos) {
                waterfall(photos);
            }
            $(window).resize(function () {
                if (photos) {
                    waterfall(photos);
                }
            });
        });
        var t_img;
        var isLoad = true;

        function isImgLoad(callback) {
            $('photos img').each(function () {
                if (this.height === 0) {
                    isLoad = false;
                    return false;
                }
            });
            if (isLoad) {
                clearTimeout(t_img);
                callback();
            } else {
                isLoad = true;
                t_img = setTimeout(function () {
                    isImgLoad(callback);
                }, 500);
            }
        }
    });
</script>

</div>
<!-- end: #wrapper -->
</body>
</html>
